<?php
/**
 * Created by PhpStorm.
 * Author: ls
 * Time: 2023/11/15 11:24
 * Desc: 退款规则方法
 */
trait Trait_Refund_Rule {

    /**
     * 保存退款规则
     * @param $product_id
     * @param $suit_id
     * @param $refund_rule
     * @return void
     */
    public function save_refund_rule($product_id, $suit_id, $refund_rule)
    {
        $refund_rule_id           = Arr::get($refund_rule, 'refund_rule_id', '');
        $refund_before_day        = Arr::get($refund_rule, 'refund_before_day', '');
        $refund_before_date       = Arr::get($refund_rule, 'refund_before_date', '');
        $refund_deduct_price_type = Arr::get($refund_rule, 'refund_deduct_price_type', '');
        $refund_deduct_price      = Arr::get($refund_rule, 'refund_deduct_price', '');

        $rule_ids = array();
        if (!empty($refund_before_day) && !empty($refund_deduct_price_type) && !empty($refund_deduct_price)) {

            foreach ($refund_before_day as $key => $value) {

                $before_day        = $value;
                $before_date       = $refund_before_date[$key] ?? '00:00';
                $rule_id           = $refund_rule_id[$key] ?? '';
                $deduct_price_type = $refund_deduct_price_type[$key] ?? 1;
                $deduct_price      = $refund_deduct_price[$key] ?? 0;

                if ($before_day != '' && $before_date != '' && $deduct_price_type != '' && $deduct_price != '') {
                    if ($rule_id) {
                        $rule_ids[] = $rule_id;
                        $model      = ORM::factory($this->_table_name, $rule_id);
                    } else {
                        $model          = ORM::factory($this->_table_name);
                        $model->addtime = time();
                    }
                    $model->product_id  = $product_id;
                    $model->suit_id     = $suit_id;
                    $model->before_day  = $before_day;
                    $model->before_date = $before_date;
                    $model->fee_type    = $deduct_price_type;
                    $model->fee         = $deduct_price;
                    $model->save();
                    if ($model->saved()) {
                        $model->reload();
                        $rule_ids[] = $model->id;
                    }
                }
            }
        }

        if (!empty($rule_ids)) {
            // 移除原有的已删除的数据
            DB::delete($this->_table_name)
                ->where('id', 'NOT IN', $rule_ids)
                ->and_where('suit_id', '=', $suit_id)
                ->execute();
        }
    }

    /**
     * 获取退款规则
     * @param $suit_id
     * @return array
     */
    public function get_refund_rule($suit_id)
    {
        return DB::select('id', 'product_id', 'suit_id', 'before_day', 'before_date', 'fee_type', 'fee', 'addtime')
            ->from($this->_table_name)
            ->where('suit_id', '=', $suit_id)
            ->order_by('before_day', 'desc')
            ->order_by('before_date', 'asc')
            ->execute()
            ->as_array();
    }
}